/*
Copyright (c) 2008,2009, David Beck

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
   @file t__xdr.cc
   @brief check if xdrbuf and AFS xdr routines are compatible
 */

#define SKIP_OPS_ONLY_TYPES

#define RT0D4_WINDOWS_H_INCLUDED
#include "xdrbuf.hh"
#include "zfile.hh"
#include "common.h"

extern "C" {
  //#include "rx/xdr.h"
  #include "t__t1.h"
}

#include <stdio.h>
#include <assert.h>

using namespace rt0d4::common;

/** @brief contains tests related to xdr routines */
namespace test_xdr {

  /** @test check if 'int' marshalled by xdrbuf is the same as xdr's */
  void test_int()
  {
    pbuf pb;
    xdrbuf xb(pb);
    XDR xdrs;
    int i = 0xbadbad;
    unsigned char tmp[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24 };
    unsigned char tm2[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24 };
    xdrmem_create( &xdrs, (char *)tmp, 24, XDR_ENCODE );
    xdr_int( &xdrs, &i );
    xdr_destroy( &xdrs );

    xb << i;
    pbuf::iterator bn(pb.begin());
    ::memcpy( tm2,(*bn)->data_,(*bn)->size_ );

    assert( memcmp( tmp,tm2,24 ) == 0 );
    assert( tmp[4] == 4 );

    int j=0;
    xb >> j;

    assert( i == j );
  }

  /** @test check if 'string' marshalled by xdrbuf is the same as xdr's */
  void test_string()
  {
    pbuf pb;
    xdrbuf xb(pb);
    XDR xdrs;
    char str[] = "HelloW";
    char * strp = ::strdup(str);
    unsigned char tmp[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24 };
    unsigned char tm2[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24 };
    xdrmem_create( &xdrs, (char *)tmp, 24, XDR_ENCODE );
    xdr_string( &xdrs, &strp, sizeof(str) );
    unsigned data_len = xdr_getpos(&xdrs);
    xdr_destroy( &xdrs );

    xb << str;

    pbuf::iterator bn(pb.begin());
    ::memcpy( tm2,(*bn)->data_,(*bn)->size_ );
    assert( memcmp( tmp,tm2,24 ) == 0 );

    unsigned int sz=0;
    mpool<> mp;
    assert( xb.get_data( mp,sz,24 ) == true );
    assert( data_len == pb.size() );
  }

  /** @test check if 'binary' marshalled by xdrbuf is the same as xdr's */
  void test_bin()
  {
    pbuf pb;
    xdrbuf xb(pb);
    XDR xdrs;

    unsigned char bin [] = { 255, 254, 253, 252, 251, 250 };
    unsigned char * binp = (unsigned char *)::malloc(6);
    ::memcpy(binp,bin,6);

    unsigned char tmp[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24 };
    unsigned char tm2[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24 };

    xdrmem_create( &xdrs, (char *)tmp, 24, XDR_ENCODE );
    opdata_t td;
    td.opdata_t_len = 6;
    td.opdata_t_val = (char *)binp;
    xdr_opdata_t( &xdrs, &td );
    unsigned data_len = xdr_getpos(&xdrs);
    xdr_destroy( &xdrs );

    xb << xdrbuf::bindata_t( binp,6 );
    pbuf::iterator bn(pb.begin());
    ::memcpy( tm2,(*bn)->data_,(*bn)->size_ );
    assert( memcmp( tmp,tm2,24 ) == 0 );

    unsigned int sz=0;
    mpool<> mp;
    assert( xb.get_data( mp,sz,24 ) == true );
    assert( data_len == pb.size() );
  }

} // end of test_xdr

using namespace test_xdr;

int main()
{
  test_int();
  test_string();
  test_bin();
  return 0;
}

/* EOF */
